Prev Next |
More complicated applications will require a state management facility. Your code-beside class is instantiated each time the User posts back, so you cannot simply store the state information in a class attribute. Fortunately, Sitecore provides the ServerSettings property in the ClientPage class for storing and retrieving state information. The ServerSettings property is a System.Web.UI.StateBag class (similar to asp.net ViewState).
Sitecore supports two kinds of state storage facilities: the file system and the SQL database (the file system by default, controlled by the ViewStateStore setting in web.config).
Perform the following steps for a simple example of storing and retrieving state information.
- Add these buttons to the layout: <Button Header="Append" Click="OnAppend" />
<Button Header="Clear" Click="OnClear" /> - Add the PersistentValue property to your code-beside class. This will hold the actual persistent value: protected string PersistentValue
{
get
{
return Context.ClientPage.ServerProperties["PersistentValue"] as string;
}
set
{
Context.ClientPage.ServerProperties["PersistentValue"] = value;
Edit.Value = value;
}
}The property stores the previous Edit value.
- Initialize the Edit control with an initial value:
// OnLoad method is fired when the form is loading,
// similar to asp.net
protected override void OnLoad(EventArgs args)
{
// This is how you can determine whether application is just
// launching or handling postback, similar to the
// Page.IsPostBack in asp.net
if (!Context.ClientPage.IsEvent)
{
PersistentValue = "Initial";
}
} - Add the button handlers: protected void OnAppend()
{
// Append current Edit value to the previous persisted one
PersistentValue = PersistentValue + "|" + Edit.Value;
}
protected void OnClear()
{
// Clear the persisted value
PersistentValue = string.Empty;
} -
Recompile, refresh and test your changes. You can use the ‘Append’ button to append the current Edit value to the previous persistent one or clear the persistent value using the ‘Clear’ button.
Prev Next